import Database, * as BetterSqlite3 from 'better-sqlite3'
import { app } from 'electron'
import { resolve } from 'path'
import config from './config'
import { existsSync } from 'node:fs'
// import path from 'node:path'
// windows系统桌面的snippet.db文件
// liunx/mac  /home/Desktop/snippet.db

const db = (): BetterSqlite3.Database => {
  let dir = resolve(app.getPath('home'), 'Desktop')
  console.log(config, 12)

  if (config.databaseDirectory && existsSync(config.databaseDirectory)) {
    dir = config.databaseDirectory.endsWith('\\')
      ? config.databaseDirectory
      : config.databaseDirectory + '\\'
  }
  // const dbPath = path.join(config.databaseDirectory, 'snippet.db')
  const db: BetterSqlite3.Database = new Database(dir + 'snippet.db', {})
  db.pragma('journal_mode = WAL')
  return db
}

export { db }
